﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;

namespace DTO
{
    public partial class FormXemThoiKhoaBieu : Form
    {
        private List<LopDTO> dsLop;

        private List<GiaoVienDTO> dsGiaoVien;

        public FormXemThoiKhoaBieu()
        {
            InitializeComponent();
        }

        private void FormXemThoiKhoaBieu_Load(object sender, EventArgs e)
        {
            LoadLop();
            LoadGiaoVien();
        }

        private void LoadLop()
        {
            LopBUS lBUS = new LopBUS();
            dsLop = lBUS.LayDanhSach2();
            this.cbLop.DataSource = dsLop;
            this.cbLop.DisplayMember = "TenLop";
            this.cbLop.ValueMember = "ID";
        }

        private void LoadGiaoVien()
        {
            GiaoVienBUS giaoVienBUS = new GiaoVienBUS();
            dsGiaoVien = giaoVienBUS.LayDanhSach2();
            this.cbGiaoVien.DataSource = dsGiaoVien;
            this.cbGiaoVien.DisplayMember = "HoTen";
            this.cbGiaoVien.ValueMember = "ID";
        }

        private void cbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbLop.SelectedIndex != -1)
            {
                LoadThoiKhoaBieuCuaLop(dsLop[cbLop.SelectedIndex]);
            }
        }

        private void LoadThoiKhoaBieuCuaLop(LopDTO lDTO)
        {
            ThoiKhoaBieuBUS tkbBUS = new ThoiKhoaBieuBUS();
            ThoiKhoaBieuDTO tkbDTO = tkbBUS.GetByLop(lDTO);
            ChiTietThoiKhoaBieuBUS cttkbBUS = new ChiTietThoiKhoaBieuBUS();
            IList<ChiTietThoiKhoaBieuDTO> list = cttkbBUS.GetListByThoiKhoaBieu(tkbDTO);

            dgvBuoiSangLop.Rows.Clear();
            while (dgvBuoiSangLop.Rows.Count < 5)
            {
                dgvBuoiSangLop.Rows.Add();
            }

            dgvBuoiChieuLop.Rows.Clear();
            while (dgvBuoiChieuLop.Rows.Count < 5)
            {
                dgvBuoiChieuLop.Rows.Add();
            }
            foreach (ChiTietThoiKhoaBieuDTO ct in list)
            {
                int cot;
                switch (ct.Thu)
                {
                    case "Thứ 2":
                        cot = 1;
                        break;
                    case "Thứ 3":
                        cot = 2;
                        break;
                    case "Thứ 4":
                        cot = 3;
                        break;
                    case "Thứ 5":
                        cot = 4;
                        break;
                    case "Thứ 6":
                        cot = 5;
                        break;
                    case "Thứ 7":
                        cot = 6;
                        break;
                    case "CN":
                        cot = 7;
                        break;
                    default:
                        cot = 0;
                        break;
                }
                int dong;
                if (ct.Tiet <= 5)
                {
                    dong = ct.Tiet - 1;
                    dgvBuoiSangLop.Rows[dong].Cells[cot].Value = ct.TenMonHoc;
                }
                else
                {
                    dong = ct.Tiet - 6;
                    dgvBuoiChieuLop.Rows[dong].Cells[cot].Value = ct.TenMonHoc;
                }
            }
        }

        private void LoadThoiKhoaBieuCuaGiaoVien(GiaoVienDTO giaoVienDTO)
        {
            ChiTietThoiKhoaBieuBUS cttkbBUS = new ChiTietThoiKhoaBieuBUS();
            IList<ChiTietThoiKhoaBieuDTO> list = cttkbBUS.GetListActiveByTeacher(giaoVienDTO);

            dgvBuoiSangGV.Rows.Clear();
            while (dgvBuoiSangGV.Rows.Count < 5)
            {
                dgvBuoiSangGV.Rows.Add();
            }

            dgvBuoiChieuGV.Rows.Clear();
            while (dgvBuoiChieuGV.Rows.Count < 5)
            {
                dgvBuoiChieuGV.Rows.Add();
            }
            foreach (ChiTietThoiKhoaBieuDTO ct in list)
            {
                int cot;
                switch (ct.Thu)
                {
                    case "Thứ 2":
                        cot = 1;
                        break;
                    case "Thứ 3":
                        cot = 2;
                        break;
                    case "Thứ 4":
                        cot = 3;
                        break;
                    case "Thứ 5":
                        cot = 4;
                        break;
                    case "Thứ 6":
                        cot = 5;
                        break;
                    case "Thứ 7":
                        cot = 6;
                        break;
                    case "CN":
                        cot = 7;
                        break;
                    default:
                        cot = 0;
                        break;
                }
                int dong;
                if (ct.Tiet <= 5)
                {
                    dong = ct.Tiet - 1;
                }
                else
                {
                    dong = ct.Tiet - 6;
                }
                dgvBuoiSangGV.Rows[dong].Cells[cot].Value = string.Format("{0} ({1})", ct.TenMonHoc, ct.TenLop);
            }
        }

        private void btnXuatLop_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.FileName = cbLop.Text;
            saveFile.Filter = "Excel file|*.xlsx|All files|*.*";
            DialogResult result = saveFile.ShowDialog();
            if (result == DialogResult.OK)
            {
                Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                app.Visible = false;
                Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                app.Visible = true;
                worksheet = workbook.Sheets["Sheet1"];
                worksheet = workbook.ActiveSheet;
                for (int i = 1; i < dgvBuoiSangLop.Columns.Count + 1; i++)
                {
                    worksheet.Cells[1, i] = dgvBuoiSangLop.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < dgvBuoiSangLop.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dgvBuoiSangLop.Columns.Count; j++)
                    {
                        if (dgvBuoiSangLop.Rows[i].Cells[j].Value != null)
                        {
                            worksheet.Cells[i + 2, j + 1] = dgvBuoiSangLop.Rows[i].Cells[j].Value.ToString();
                        }
                    }
                }

                for (int i = 0; i < dgvBuoiChieuLop.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dgvBuoiChieuLop.Columns.Count; j++)
                    {
                        if (dgvBuoiChieuLop.Rows[i].Cells[j].Value != null)
                        {
                            worksheet.Cells[i + 7, j + 1] = dgvBuoiChieuLop.Rows[i].Cells[j].Value.ToString();
                        }
                    }
                }
                workbook.SaveAs(saveFile.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                app.Quit();
            }
        }

        private void cbGiaoVien_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbLop.SelectedIndex != -1)
            {
                LoadThoiKhoaBieuCuaGiaoVien(dsGiaoVien[cbGiaoVien.SelectedIndex]);
            }
        }

        private void btnXuatGV_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.FileName = cbGiaoVien.Text;
            saveFile.Filter = "Excel file|*.xlsx|All files|*.*";
            DialogResult result = saveFile.ShowDialog();
            if (result == DialogResult.OK)
            {
                Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
                app.Visible = false;
                Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
                Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
                app.Visible = true;
                worksheet = workbook.Sheets["Sheet1"];
                worksheet = workbook.ActiveSheet;
                for (int i = 1; i < dgvBuoiSangGV.Columns.Count + 1; i++)
                {
                    worksheet.Cells[1, i] = dgvBuoiSangLop.Columns[i - 1].HeaderText;
                }
                for (int i = 0; i < dgvBuoiSangGV.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dgvBuoiSangGV.Columns.Count; j++)
                    {
                        if (dgvBuoiSangGV.Rows[i].Cells[j].Value != null)
                        {
                            worksheet.Cells[i + 2, j + 1] = dgvBuoiSangGV.Rows[i].Cells[j].Value.ToString();
                        }
                    }
                }

                for (int i = 0; i < dgvBuoiChieuGV.Rows.Count - 1; i++)
                {
                    for (int j = 0; j < dgvBuoiChieuGV.Columns.Count; j++)
                    {
                        if (dgvBuoiChieuGV.Rows[i].Cells[j].Value != null)
                        {
                            worksheet.Cells[i + 7, j + 1] = dgvBuoiChieuGV.Rows[i].Cells[j].Value.ToString();
                        }
                    }
                }
                workbook.SaveAs(saveFile.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                app.Quit();
            }
        }
    }
}
